package cn.bnuz.edu.liyang.wait_eat_what;

import android.app.Instrumentation;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Best extends AppCompatActivity {
    ListView bestlist;
    private List<Map<String,Object>> things = new ArrayList<Map<String,Object>>();
    private List<Map<String,Object>> things10 = new ArrayList<Map<String,Object>>();
    SimpleAdapter adapter;
    SQLiteDatabase db;
    ContentValues values;
    DataBase dbHelper = new DataBase(this,"Thing",null,1);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.best);

        //返回按钮
        Button backbutton = (Button)findViewById(R.id.backbutton);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation instrument = new Instrumentation();
                            instrument.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                        }            }        }.start();    }});

        //点击人像按钮弹出登录界面
        ImageButton loginbutton = (ImageButton) findViewById(R.id.loginbuttom_best);
        loginbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Best.this,Login.class);
                startActivity(intent);
            }
        });

        //数据库
        db = dbHelper.getWritableDatabase();
//        values = new ContentValues();
//        values.put("name","芒果冰冰");
//        values.put("fromm","东北人家");
//        values.put("img_url",R.drawable.xban0);
//        values.put("adress","学三二楼");
//        values.put("price","6.0");
//        values.put("stars","4.5");
//        db.insert("Thing",null,values);
//        db.delete("Thing","name=?",new String[]{"红烧猪脚"});
        bestlist =findViewById(R.id.best_listview);
        Cursor cursor = db.query("Thing",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                Map<String,Object> map = new HashMap<String,Object>();
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String from = cursor.getString(cursor.getColumnIndex("fromm"));
                String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
                String adress = cursor.getString(cursor.getColumnIndex("adress"));
                Double price = cursor.getDouble(cursor.getColumnIndex("price"));
                Double stars = cursor.getDouble(cursor.getColumnIndex("stars"));
                map.put("name",name);
                map.put("from",from);
                map.put("img_url",img_url);
                map.put("adress",adress);
                map.put("price",price);
                map.put("stars",stars);


                things.add(map);
            }while(cursor.moveToNext());
            Collections.sort(things, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> stringObjectMap, Map<String, Object> t1) {
                    Double s1 =(Double)stringObjectMap.get("stars");
                    Double s2 =(Double)t1.get("stars");
                    return s2.compareTo(s1);
                }
            });
            for(int i=0;i<10 && i<cursor.getCount();i++){
                Map<String,Object> map = things.get(i);
                switch (i){
                    case 0:
                        map.put("index",R.drawable.ic__1);
                        break;
                    case 1:
                        map.put("index",R.drawable.ic__2);
                        break;
                    case 2:
                        map.put("index",R.drawable.ic__3);
                        break;
                    case 3:
                        map.put("index",R.drawable.ic__4);
                        break;
                    case 4:
                        map.put("index",R.drawable.ic__5);
                        break;
                    case 5:
                        map.put("index",R.drawable.ic__6);
                        break;
                    case 6:
                        map.put("index",R.drawable.ic__7);
                        break;
                    case 7:
                        map.put("index",R.drawable.ic__8);
                        break;
                    case 8:
                        map.put("index",R.drawable.ic__9);
                        break;
                    case 9:
                        map.put("index",R.drawable.ic__10);
                        break;
                }
                things10.add(map);
            }
        }

        adapter = new SimpleAdapter(this,things10,R.layout.best_list_item,new String[]{"name","from","img_url","stars","index"},new int[]{R.id.best_name,R.id.best_from,R.id.best_img,R.id.best_star,R.id.best_num});
        bestlist.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Best.this,Detail.class);
                Bundle bundle = new Bundle();
                TextView name = (TextView)view.findViewById(R.id.best_name);
                TextView fromm = (TextView)view.findViewById(R.id.best_from);
                String name1 = (String) name.getText();
                String fromm1 = (String) fromm.getText();
                bundle.putString("name",name1);
                bundle.putString("from",fromm1);
                intent.putExtra("detail",bundle);
                startActivity(intent);
            }
        });
        bestlist.setAdapter(adapter);
    }
}
